{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "faa69863-160e-4bb1-951b-1451cc199442",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.0865727 , 0.61079563, 0.91698954, 0.67652785, 0.2702073 ])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "# 生成一个随机数组\n",
    "n1=np.random.rand(5)  # [0, 1)\n",
    "n1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5ad7b4a8-4172-4d84-88ee-c8d15d9a5509",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.1674993 , 0.29737337, 0.01263773, 0.3686081 ],\n",
       "       [0.01480158, 0.70629417, 0.97893028, 0.42330063]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n2=np.random.rand(2, 4)\n",
    "n2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e345ade9-9f1e-46cd-a42b-431d5b2e6212",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.72220218, -0.83579988, -1.2430094 ])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用于从正态分布中返回随机生成的数组\n",
    "n3=np.random.randn(3)\n",
    "n3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4aefa31e-f40a-412c-b59d-cb95287144c5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1.43212651,  0.40604994, -0.22686972, -0.25265857],\n",
       "       [ 0.63550314,  0.14045693, -1.24022491, -0.32790064],\n",
       "       [-0.8314568 , -1.14920539, -0.06131859, -0.48231659]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n3=np.random.randn(3, 4)\n",
    "n3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6a533540-4fef-4671-af8e-6515ec0a5d94",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 2, 1, 1, 2, 2, 2, 2, 2], dtype=int32)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生成指定范围的随机数组\n",
    "n4=np.random.randint(1, 3, 10)  # [1, 3)，产生10个元素\n",
    "n4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ca7201de-f5fc-4bc4-9054-0ac203bc286e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 2, 1],\n",
       "       [2, 1, 2]], dtype=int32)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n5=np.random.randint(1, 3, size=(2, 3))\n",
    "n5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2c42f594-930a-4070-9539-f69f33203b8e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.12621669, -0.16255014, -0.15142144,  0.05962044, -0.06695546,\n",
       "       -0.00101652, -0.07987115, -0.072993  ,  0.17336855, -0.02380385])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生成正态分布的随机数组\n",
    "n6=np.random.normal(0, 0.1, 10)\n",
    "n6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "83c8aa77-5fa8-4e82-9119-222cc714dd11",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.02324269, -0.1576585 ,  0.23992353],\n",
       "       [-0.17077535, -0.07007979,  0.04733641]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n6=np.random.normal(0, 0.1, size=(2,3))\n",
    "n6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "bd0d2f62-d71f-4d3c-87d6-bcc101541f1e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# asarray函数的使用, 通过列表的元组创建数组\n",
    "n7 = np.asarray([(1, 2, 3), (4, 5, 6)])\n",
    "n7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "63c3f643-e872-47a8-8e46-9d5a3bb900fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过元组创建数组\n",
    "n8=np.asarray((1, 2, 3))\n",
    "n8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "81ff043d-0791-4d50-8ecd-93ec1f5c1c3b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过元组的列表\n",
    "n9=np.asarray(([1, 2], [3, 4]))\n",
    "n9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "47f1640c-739c-4f7b-af51-23aae9facbb7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([b'j', b'u', b'a', b'n', b'z', b'i', b'j', b'i', b'e'], dtype='|S1')"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 动态数组\n",
    "n1 = np.frombuffer(b'juanzijie', dtype='S1')  # S1表示单个字符串为一个字符\n",
    "n1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "fc68fb99-a0ea-46c9-b47c-e1f08e9fd8a0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<generator object <genexpr> at 0x000002323934C5F0>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 从迭代对象中从创建数组对象\n",
    "iter=(i for i in range(5))\n",
    "iter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d5065bd9-19db-4708-800c-09ac2bce9cbb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[            0, 2977902153808],\n",
       "       [2977902153232, 2977902155216]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# empty_like的使用\n",
    "n = np.empty_like([[1, 2], [3, 4]])  # 创建一个2行2列的数组，因为给定的是2行2列\n",
    "n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5ab63d68-4314-44a5-b531-5096ed6b1107",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0],\n",
       "       [0, 0]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = np.zeros_like([[1, 2], [3, 4]])  # 创建以0填充的2行2列的数组\n",
    "n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a7650222-b86c-4fb9-8849-a844b53f084f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1],\n",
       "       [1, 1]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n=np.ones_like([[1, 2], [3, 4]])  # 创建以1填充的2行2列\n",
    "n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "6e505662-d56e-48a2-919b-4be289c51d4f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8, 8],\n",
       "       [8, 8]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n=np.full_like([[1, 2], [3, 4]], 8)  # 创建以8填充的数组\n",
    "n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8770279b-32a4-43f8-99e5-2c982b5658bc",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bd37292a-5082-4064-ad2c-868ec44fd1d3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
